package com.ddtech.business.check.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddtech.business.check.entity.Check;
import com.ddtech.business.check.entity.CheckUserRecord;
import com.ddtech.business.check.mapper.CheckMapper;
import com.ddtech.business.check.mapper.CheckUserRecordMapper;
import com.ddtech.business.check.query.CheckUserRecordQuery;
import com.ddtech.business.check.service.CheckUserRecordService;
import com.ddtech.business.constant.Del;
import com.ddtech.utils.PageUtil;
import com.ddtech.utils.SecurityUtils;
import com.ddtech.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 学员考核记录表 服务实现类
 * </p>
 *
 * @author bin_pu
 * @since 2023-02-06
 */
@Service
public class CheckUserRecordServiceImpl extends ServiceImpl<CheckUserRecordMapper, CheckUserRecord> implements CheckUserRecordService {
    @Resource
    private CheckMapper checkMapper;

    @Resource
    private CheckUserRecordMapper checkUserRecordMapper;

    @Override
    public void saveCheckUser(Check check, List<CheckUserRecord> checkUser) {
        LambdaQueryWrapper<CheckUserRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CheckUserRecord::getCheckId, check.getId());
        queryWrapper.eq(CheckUserRecord::getIsDelete, Del.FALSE.getStatus());
        List<CheckUserRecord> userRecordList = this.list(queryWrapper);

        List<CheckUserRecord> collect = userRecordList.stream().filter(item -> !checkUser.contains(item)).collect(Collectors.toList());
        for (CheckUserRecord checkUserRecord : collect) {
            checkUserRecord.baseDelete();
            this.saveOrUpdate(checkUserRecord);
        }
        checkUser.forEach(checkUserRecord -> {
            checkUserRecord.setCheckId(check.getId());
            checkUserRecord.setCheckType(check.getCheckType());
            checkUserRecord.init();
            checkUserRecord.setIsFinish(0);
            this.saveOrUpdate(checkUserRecord);
        });
    }

    @Override
    public List<CheckUserRecord> getCheckUser(String checkId) {
        LambdaQueryWrapper<CheckUserRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CheckUserRecord::getCheckId, checkId);
        queryWrapper.eq(CheckUserRecord::getIsDelete, Del.FALSE.getStatus());
        return this.list(queryWrapper);
    }

    @Override
    public void deleteCheckUser(String checkId) {
        LambdaQueryWrapper<CheckUserRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CheckUserRecord::getCheckId, checkId);
        queryWrapper.eq(CheckUserRecord::getIsDelete, Del.FALSE.getStatus());
        List<CheckUserRecord> list = this.list(queryWrapper);
        for (CheckUserRecord checkUserRecord : list) {
            checkUserRecord.baseDelete();
            this.saveOrUpdate(checkUserRecord);
        }
    }

    @Override
    public Object pageList(CheckUserRecordQuery checkUserRecordQuery) {
        Check check = checkMapper.selectById(checkUserRecordQuery.getCheckId());
        QueryWrapper<CheckUserRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("check_id", checkUserRecordQuery.getCheckId());
        queryWrapper.eq("is_delete", Del.FALSE.getStatus());
        if (StringUtils.isNotBlank(checkUserRecordQuery.getUserName())) {
            queryWrapper.like("user_name", checkUserRecordQuery.getUserName());
        }
        switch (checkUserRecordQuery.getType()) {
            case 2:
                queryWrapper.ge("source", check.getStandardScore());
                break;
            case 3:
                queryWrapper.le("source", check.getStandardScore());
                break;
            case 4:
                queryWrapper.isNull("is_finish");
                break;
        }
        queryWrapper.last("ORDER BY IF(source IS NULL,0,source)  DESC ");
        IPage page = this.page(checkUserRecordQuery.populatePage(), queryWrapper);
        return PageUtil.toPage(page.getRecords(), page.getTotal());
    }

    /**
     * description:APP考试库首页考试数据统计
     * @param:  * @param
     * @date: 2023-07-04
     * @return: java.lang.Object
     * @version v1.0
     * @author:wei_zhang(xiao)
     */
    @Override
    public Object getStatisticsApp() {
        return  checkUserRecordMapper.getStatisticsApp(SecurityUtils.getUserId());
    }

    /**
     * description:APP考试库首页考试数据统计
     * @param:  * @param
     * @date: 2023-07-04
     * @return: java.lang.Object
     * @version v1.0
     * @author:wei_zhang(xiao)
     */
    @Override
    public Object checkPlanStatisticsApp() {
        return  checkUserRecordMapper.checkPlanStatisticsApp(SecurityUtils.getUserId());
    }
}
